<?php
	class ArticleAction extends BaseAction{
		
		private $fileName;//上传图片名字，用上传图片的大小的md5值作为其名字
		public function index(){
			$article = M("Article");
			import("ORG.Util.Page");
			//显示所有分类
			$cat = M("Category");
			$category = $cat->field("category_name,id,pid,concat(path,'-',id) as bpath")
							->order('bpath')
							->select();
			$this->getCategoryFormat($category);
			$this->assign('category',$category);
			//筛选各分类下的文章
			if(empty($_GET) || 0 == $_GET['category']){
				$count = $article->count();
			}else{
				$where = 'category in '.'(select id from zgcms_category where id = '.$_GET['category'].' or pid = '.$_GET['category'].' or path like "%-'.$_GET['category'].'-%")';
				$count = $article->where($where)->count();
				$this->assign('cat',$_GET['category']);
			}
			$page = new Page($count,c('PAGESIZE'));
			$show = $page->show();
			$data = $article->limit($page->firstRow.','.$page->listRows)
							->field("zgcms_article.id,title,attr,count,date,category_name")
							->where($where)
							->join("zgcms_category on zgcms_article.category = zgcms_category.id")
							->select();
			$this->assign("list",$data);
			$this->assign('page',$show);
			$this->display();
		}
		
		public function add(){
			$cat = M("Category");
			$parent = $cat->field("category_name,id,pid,concat(path,'-',id) as bpath")
						->order('bpath')
						->select();
			$this->getCategoryFormat($parent);
			//格式化分类名称.
			$this->getCategoryFormat($parent);
			$this->assign("parent",$parent);
			$this->display();
		}
		
		public function insert(){
			$article = D("Article");
// 			dump($_POST);
// 			exit();
			if($data = $article->create())
			{
				if($article->add())
				{
					$this->assign("jumpUrl","__URL__");
					$this->success("添加成功！");
				}else{
					$this->error("添加失败！");
				}
			}else{
				$this->error($article->getError());
			}	
		}
		
		//通过js调用这个页面，用于选择文件上传
		public function showUploadFile(){
			$this->display();
		}
		
		//接受上传操作，并将上传结果返回，取得上传路径并显示上传图片
		public function upload(){
// 			dump($_FILES);
// 			//exit();
// 			exit();
			$this->fileName = md5_file($_FILES['pic']['tmp_name']);
			if(!empty($_FILES)){
				$result = $this->_upload();
				if(isset($result)){
					//echo "<script type='text/javascript'>alert('上传图片成功！');</script>";
					//$path = '../../Public/Uploads/s_'.$result[0]['savename'];
					$path = C('WEB_DIR').'/Public/Uploads/s_'.$result[0]['savename'];
					$name = $result[0]['savename'];
					echo "<script type='text/javascript'>opener.document.content.thumbnail.value='$name';</script>";
					echo "<script type='text/javascript'>opener.document.content.pic.style.display='block';</script>";
					echo "<script type='text/javascript'>opener.document.content.pic.src='$path';</script>";
					$this->success('上传文件成功！');
					echo "<script type='text/javascript'>window.close();</script>";
					exit();
					
				}else{
					$this->error('上传文件失败！');
				}
			}else{
				$this->error('请选择要上传的文件！');
			}
		}
		
		//处理上传过程
		private function _upload(){
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();	
			$upload->maxSize = 204800;//上传文件最大大小
			$upload->savePath = './Public/Uploads/';//上传文件保存路径
			$upload->saveRule = $this->fileName;//设置文件名保存规则
			$upload->allowExts=array('jpg','jpeg','png','gif');//设置允许的文件后缀
			$upload->allowTypes=array('image/png','image/jpg','image/pjpeg','image/gif','image/jpeg','image/x-png','image/x-jpg','image/x-pjpeg','image/x-gif','image/x-jpeg');  //检测mime类型
			$upload->thumb=true;   //是否开启图片文件缩略
			$upload->thumbMaxWidth='150';  //以字串格式来传，如果你希望有多个，那就在此处，用,分格，写上多个最大宽
			$upload->thumbMaxHeight='100';	//最大高度
			$upload->thumbPrefix='s_';//缩略图文件前缀
			$upload->uploadReplace=true;
			if($upload->upload()){
				return $upload->getUploadFileInfo();
			}else{
				//echo $upload->getErrorMsg();
				$this->error($upload->getErrorMsg());
			}
		}
		
		//用于ckeditor文件上传处理。
		public function checkUp(){
			//echo C('WEB_DIR').'/Public/Uploads/';
// 			exit();
			$this->fileName = md5_file($_FILES['upload']['tmp_name']);
			if(isset($_GET['type'])){
				$result = $this->_upload();
				if(isset($result)){
					$_path = C('WEB_DIR').'/Public/Uploads/'.$result[0]['savename'];
					$_ckefn = $_GET['CKEditorFuncNum'];
					echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($_ckefn,\"$_path\",'图片上传成功！');</script>";
				}else{
					$this->error('上传文件失败！');
				}
			}else{
				$this->error('非法操作！上传失败！');
			}
		}
		
		public function edit(){
			if(!empty($_GET['id'])){
				$art = M('Article');
				$data = $art->field(true)->getById($_GET['id']);
// 				dump($data);
				if(null == $data){
					$this->error('请确认要修改文章的ID正确！');
				}else{
					$cat = M("Category");
					$parent = $cat->field("category_name,id,pid,concat(path,'-',id) as bpath")
								  ->order('bpath')
								  ->select();
					$this->getCategoryFormat($parent);
					//格式化分类名称.
					$this->getCategoryFormat($parent);
					$this->assign("parent",$parent);
					$this->assign('art',$data);
					$this->display();
				}
			}else{
				$this->error('请选择要修改的文章！');
			}
			
		}
		
		public function update(){
			$art = D('Article');
			if($art->create()){
				if($art->save()){
					$this->success('修改文章成功！');
				}else{
					$this->error('无修改内容或修改文章失败！');
				}
			}else{
				$this->error($art->getError());
			}
		}
		
		public function delete(){
			if(!empty($_GET['id']))
			{
				$art = M("Article");
				if($art->delete($_GET['id'])){
					$this->success("删除文章成功!");
				}else{
					$this->error("删除文章失败!");
				}
			}else{
				$this->error("请选择要删除的文章！");
			}
		}
		
		//格式化分类名称，树形分类。
		private function getCategoryFormat(&$data){
			foreach($data as $key => $val){
				$data[$key]['fname'] = 0 == $val['pid'] ? $val['category_name'] : str_repeat('&nbsp;', (count(explode('-', $val['bpath']))-1)*4).'|--'.$val['category_name'];
			}
		}
		
	}
?> 